package com.cndw.kungfu.comm;

import java.util.Map;
import java.util.UUID;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.JsonProcesser;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.openqq.api.ApiClient;

public class CommInspection {

	public static final String ret = "ret";

	public static final String captchaVerifyRight = "\"is_right\": 1";

	public static final String commRight = "\"ret\": 0";

	@Deprecated
	protected static final String spamResult = "result";

	public static final String spamWrong = "\"result\": 1";
	
	public static final String filterDirty = "\"is_dirty\": 0";
	
	public static final String msgKey = "msg";

	public static String checkFilter(Player player, String msg) {
		if (ConstantsConfig.isInspectComm(Constants.inspectFilter)) {
			if (!player.getDataInfo().isGmLogin()) {
				if (ConstantsConfig.isLocalHost()) {
					ApiClient.setHostTest(true);
				}
				String msgId = UUID.randomUUID().toString();
				String apiMsg = ApiClient.checkFilter(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(),
						player.getLastIp(), msg, msgId);
				if (apiMsg != null && apiMsg.indexOf(commRight) >= 0) {
					if (apiMsg.indexOf(filterDirty) >=0) {
						return msg;
					}
					try {
						Map<String, String> map = JsonProcesser.objectJsonMapDecode(apiMsg, String.class, String.class);
						if (map.containsKey(msgKey)) {
							return map.get(msgKey);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return msg;
	}

	public static boolean checkSpam(Player player, String msg) {
		if (!ConstantsConfig.isInspectComm(Constants.inspectChat)) {// 不检查聊天则返回不含敏感字
			return false;
		}
		if (!player.getDataInfo().isGmLogin()) {
			if (ConstantsConfig.isLocalHost()) {
				ApiClient.setHostTest(true);
			}
			String apiMsg = ApiClient.checkSpam(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(), player.getLastIp(),
					msg);
			if (apiMsg != null) {
				//LogUtil.getMain().debug(apiMsg);
				try {
					if (apiMsg.indexOf(spamWrong) >= 0) {
						return true;
					}
				} catch (Exception e) {
					LogUtil.getQQDC().debug(e.getMessage());
				}
			}
		}
		return false;
	}

	public static void isLogin(Player player) {
		if (!player.getDataInfo().isGmLogin()) {
			if (ConstantsConfig.isLocalHost()) {
				ApiClient.setHostTest(true);
			}
			String msg = ApiClient.isLogin(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(), player.getLastIp());
			if (msg == null || msg.indexOf(commRight) == -1) {
				// LogUtil.getQQDC().debug(msg);
			}

		}
	}

	public static Object[] captchaGet(Player player) {
		if (ConstantsConfig.isLocalHost()) {
			ApiClient.setHostTest(true);
		}
		Object[] apiMsg = ApiClient.captchaGet(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(), player.getLastIp());
		return apiMsg;
	}

	public static boolean captchaVerify(Player player, String verifysig, String verifycode) {
		if (ConstantsConfig.isLocalHost()) {
			ApiClient.setHostTest(true);
		}
		String apiMsg = ApiClient.captchaVerify(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(), player.getLastIp(),
				verifysig, verifycode);
		if (apiMsg == null || apiMsg.indexOf(captchaVerifyRight) == -1) {
			LogUtil.getQQDC().debug(apiMsg);
			return false;
		}

		return true;
	}

	public static Map<String, String> isVip(Player player) {
		if (ConstantsConfig.isLocalHost()) {
			ApiClient.setHostTest(true);
		}
		String apiMsg = ApiClient.isVip(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(), player.getLastIp());
		try {
			Map<String, String> map = JsonProcesser.objectJsonMapDecode(apiMsg, String.class, String.class);
			if (map != null && map.containsKey(ret) && Integer.valueOf(map.get(ret).toString()) == 0) {
				return map;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Map<String, String> totalVipGet(Player player) {
		if (ConstantsConfig.isLocalHost()) {
			ApiClient.setHostTest(true);
		}
		String apiMsg = ApiClient.totalVipGet(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(), player.getLastIp(), 1);
		try {
			Map<String, String> map = JsonProcesser.objectJsonMapDecode(apiMsg, String.class, String.class);
			if (map != null && map.containsKey(ret) && Integer.valueOf(map.get(ret).toString()) == 0) {
				return map;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static int[] pubacct(Player player){
		if (ConstantsConfig.isLocalHost()) {
			ApiClient.setHostTest(true);
		}
		int[] pubacctArr = new int[3];
		String apiMsg = ApiClient.pubacct(player.getAccountName(), player.getDataInfo().getOpenKey(), player.getDataInfo().getPf(), player.getLastIp(), player.getDataInfo().getPfKey(),TimeProcesser.getUnixTime());
		if (apiMsg != null) {
			try {
				Map<String, String> map = JsonProcesser.objectJsonMapDecode(apiMsg, String.class, String.class);
				if (map != null) {
					if (map.containsKey("gold")) {
						pubacctArr[0] = Integer.valueOf(map.get("gold"));
					}
					if (map.containsKey("silver")) {
						pubacctArr[1] = Integer.valueOf(map.get("silver"));
					}
					if (map.containsKey("cuprum")) {
						pubacctArr[2] = Integer.valueOf(map.get("cuprum"));
					}					
					return pubacctArr;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return pubacctArr;
			
	}
	
	public static String taskCompleted(Player player,int inform_type,String contractid,int zoneid){
		String openId = player.getAccountName();
		String openKey = player.getDataInfo().getOpenKey();
		String pf = player.getDataInfo().getPf();
		String userIp = player.getLastIp();
		String pfkey = player.getDataInfo().getPfKey();
		int ts = TimeProcesser.getUnixTime();
		String apiMsg = ApiClient.taskCompleted(openId, openKey, pf, userIp, pfkey, ts, inform_type, contractid, zoneid);
		return apiMsg;
	}

}
